
import {Tooltip} from "view-design";
import Vue from "vue";

// 页码相关
let page = {
  total: 1,
  startIndex: 0,
  stepNumber: 40,
  currentPage: 1
};

// 页面的数据源
let data = {
  couponDetailList: [],
  roleList:[],
  roleTreeData:[]
};
let status = {
  //新建或编辑的弹出框
  modalView: {
    isShow: false,
    isModify: false,
    title:"优惠券详情",
    isLoading: true,
  }
};


let table =function(self){ return [
  {
    type: 'index',
    width: 55,
    fixed: 'left'
  },
  {
    title: '优惠券标题',
    key: 'title',
    width: 180,
    align: 'center'
  },
  {
    title: '优惠券类型',
    key: 'type',
    width: 120,
    align: 'center',
    render: (h, params) => {
      let type = params.row.type;
      let typeItem = self.couponTypeDict.filter(item=>item.value===type);
      let isPrimary = type===200;
      return (
        <div>
          <tag color={isPrimary?'green':'magenta'}>{isPrimary?'折扣券':'现金抵扣券'}</tag>
        </div>
      );
    },
  },
  // {
  //   title: '来源',
  //   key: 'distributionMethod',
  //   width:100,
  //   align: 'center',
  //   render: (h, params) => {
  //     let couponTypeDict = self.couponDistriubtionMethodDict;
  //     let type = params.row.distributionMethod;
  //     let typeItem = couponTypeDict.filter(item=>item.value==type);
  //     return h('div', typeItem[0].name);
  //   },
  // },
  {
    title: '状态',
    key: 'state',
    width: 110,
    align: 'center',
    render: (h, params) => {
      let statusName =  self.getMapKey(self.couponDetailStateDict, params.row.state);
      let fontColor = "green";
      switch( params.row.state ){
        case 100:
          fontColor = "grey";
          break;
        case 200:
          fontColor = "green";
          break;
        case 300:
          fontColor = "blue";
          break;
        case 400:
          fontColor = "magenta";
          break;
        default:
          break;
      }
      return (<div><tag color={fontColor}>{statusName}</tag></div>);
    }
  },
  {
    title: '发放方式',
    key: 'state',
    width: 110,
    render: (h, params) => {
      return h('div',  params.row.distributionMethod===100?'用户兑换':'后台发放');
    },
    align: 'center'
  },
  {
    title: '折扣',
    key: 'discount',
    width: 180,
    align: 'center',
    render: (h, params) => {
      let discountValue = "";
      let couponType = params.row.type;
      if(couponType===100){
        discountValue = '$'+params.row.faceValue;
      }else if(couponType == 200){
        discountValue = (100-params.row.discountRate*100).toFixed(1)+"%";
      }
      return h('div','满 $'+params.row.threshold+' 减 '+ discountValue)
    }
  },
  {
    title: '用户名',
    key: 'userEncodeName',
    width: 160,
    align: 'center',
    render:(h, params)=>{
      return h('span',params.row.userEncodeName);
    }
  },
  {
    title: '发放/兑换时间',
    key: 'validityPeriod',
    width: 180,
    align: 'center',
    render:(h,params)=>{
      let date = new Date(params.row.createtime);
      return h('div', self.$tiyBJTime(params.row.createtime));
    }
  },
  {
    title: '有效期截止',
    key: 'validityPeriod',
    width: 180,
    align: 'center',
    render:(h,params)=>{
      return h('div', self.$tiyBJTime(params.row.endtime));
    }
  },
  {
    title: '综合操作',
    key: 'action',
    align: 'center',
    width: 240,
    render: (h, params) => {
      return h('div', [
        self.$authority("COUPONS-2-201")?h('Button', {
          props: {type: 'primary', size: 'small', icon: "md-create",},
          style: {marginRight: '10px', marginTop: '6px', marginBottom: '6px'},
          on: {
            click: () => {
              self.showCouponDetailAction(params.index)
            }
          }
        }, "详情"):"",
        self.$authority("COUPONS-2-203")?h('Button', {
          //是后台发放并且未被使用的优惠券才可被撤回
          props: {type: 'error', size: 'small', icon: "md-return-left", disabled: !(params.row.distributionMethod==200&&(params.row.state==100||params.row.state==200))},
          style: {marginRight: '10px', marginTop: '6px', marginBottom: '6px'},
          on: {
            click: () => {
              self.revokeCoupon(params.index);
            }
          }
        }, "撤回"):"",
      ])
    }
  }
]
}

// 和网络交互相关的参数
let params = {
  //搜索条件
  couponType: 0,
  couponTitle: '',
  couponDetailId:'',
  userName:'',
  couponDetail:{
    id:'',
    name:'',
    title:'',
    type:100,
    state:0,
    distributionMethod:'',
    discountRate:0,
    faceValue:0,
    validityPeriod:0,
    distributionRoles:[],
    threshold:0,
    couponCode:[]
  },
  isGetAll:false //是否查询所有的优惠券
};

export  default  {
  tPage: page,
  tParam: params,
  tStatus:status,
  tData: data,
  tTable: table
};

